/*
Класс для работы с элементом автозаполнения городов
*/

var City = {

'default_text': Dictionary.translate('lang_city_autocomplete_default_text'),

'liFormatCity': function(row,i,num){
	return row[0]+'<br><span style="color:#AAA;font-size:80%;white-space:nowrap;">'+row[1]+' - '+row[2]+'</span>';
},

'onItemSelect':{},

'initCantonCity': function(canton,city,area){
	//alert(canton+','+city);
	$("#id_CANTON_ID"+area).val(canton);
	$("#id_CITY_ID"+area).val(city);
	$("#id_CITY_ID"+area).change();
},

'checkEnter': function(e){
	var kC = window.event 
		? event.keyCode 
		:e && e.keyCode 
			? e.keyCode 
			: e && e.which 
				? e.which 
				: null;
	return (kC ? kC == 13 : false);
},

'getCityValue': function(obj){
	return (obj.val() == this.default_text ? '' : obj.val());
},


'cityValueKeypress': function(obj,area,e){
	if (City.checkEnter(e)) return false;
},

'cityValueChange': function(obj,area){
	City.initCantonCity('', '',area);
},

'cityValueFocus': function(obj,area){
	if ( obj.val() == this.default_text )
		obj.val('');
	obj.removeClass('input-select');
	obj.addClass('input-select-a');
	obj.select();
},

'cityValueBlur': function(obj,area){
	var id = obj.attr('id');
	setTimeout("if ( $('#id_CANTON_ID"+area+"').val() == '' )$('#"+id+"').val('"+this.default_text+"');", 200);
	obj.removeClass('input-select-a');
	obj.addClass('input-select');
},

'init': function(id,area,value,withfo){
	withfo = 0;
	this.onItemSelect.area = function(li){ eval("City.initCantonCity( ( (!!li.extra) ? li.extra[2] : li.selectValue ), ( (!!li.extra) ? li.extra[3] : li.selectValue ), '"+area+"');") };
	$("#"+id+area)
		.val((value!=''?value:this.default_text));
	$("#"+id+area)
		.bind('keypress',function(e){return City.cityValueKeypress($(this),area,e);})
		.bind('change',function(e){return City.cityValueChange($(this),area);})
		.bind('focus',function(e){return City.cityValueFocus($(this),area);})
		.bind('blur',function(e){return City.cityValueBlur($(this),area);})
		.autocomplete("/common/city_autocomplete.php", {
			extraParams:{'withfo':withfo},
			delay:400,
			minChars:1,
			matchSubset:1,
			autoFill:false,
			matchContains:1,
			cacheLength:10,
			selectFirst:true,
			formatItem:City.liFormatCity,
			onItemSelect:City.onItemSelect.area,
			maxItemsToShow:215
		});
}

}